package com.syx.rabbitmqspring.Controller;

import com.syx.rabbitmqspring.Constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/producer")
@RestController
public class ProducerController {

    @Resource
    RabbitTemplate rabbitTemplate;


    @RequestMapping("/work")
    public String work(){
        //发送消息
        /*
        *void convertAndSend(String exchange//交换机名字, String routingKey//队列名字, Object message发送信息) throws AmqpException;
        *
        *使用内置交换机，routingkey和和队列名字一样,
        *
        * */
        for (int i = 0; i <100; i++) {
            rabbitTemplate.convertAndSend("" , Constants.WORK_QUEUE,"hello mq hello spring"+i);
        }

        return "发送成功";
    }

    //广播模式
    @RequestMapping("/fanout")
    public String fanout(){

        for (int i = 0; i <10 ; i++) {
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","hello mq hello ");
        }

        return "发送成功";
    }

    @RequestMapping("/routing")
    public String routing(){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"orange","hello orange");
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"black","hello black");
        return "发送成功";



    }

    @RequestMapping("/topic")
    public String topic(){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,"ar.orange.ar","hello orange");
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,"aa.aa.rabbit","hello black");
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,"lazy.a.a.a.a.","hello black");
        return "发送成功";



    }







}
